package com.webex.ta.hydra.core;

import com.webex.ta.hydra.core.macro.MacroDefinition;
import com.webex.ta.hydra.exceptions.IllegalKeywordException;
import com.webex.ta.hydra.exceptions.InvalidMacroException;

import java.util.Vector;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-8-31
 * Time: 9:21:17
 */
public class TestPlanValidator {

    public static boolean validateTestPlan(TestPlan tp) {
        return validateCommand(tp.getRoot());
    }

    private static boolean tryKeyword(Command cmd) throws IllegalKeywordException {

        Keyword kw = KeywordRegistry.getInstance().createKeyword(cmd.getKeyword());
//        cmd.removeValidationInfo(Integer.valueOf(0));
        return kw.validateCommand(cmd);
    }


    private static boolean tryMacro(Command cmd) throws InvalidMacroException {
        boolean pass = true;
        String macroName = cmd.getKeyword();
        MacroDefinition def = KeywordRegistry.getInstance().getMacroDefinition(macroName);
        if (def == null)
            throw new InvalidMacroException();

//        cmd.removeValidationInfo(Integer.valueOf(0));
        Vector<ParamDescriptor> descriptors = def.getFields();
        if (descriptors != null && descriptors.size() > 0) {
            for (int i = 0; i < descriptors.size(); i++) {
                if (i < cmd.getParameterSize()) {
                    String param = cmd.getParameter(i);
                    if (param == null || param.equals("")) {
                        cmd.putValidationInfo(Integer.valueOf(i + 1),
                                "Parameter missing");
                        pass = false;
                    } else {
//                        cmd.removeValidationInfo(Integer.valueOf(i + 1));
                    }
                } else {
                    cmd.putValidationInfo(Integer.valueOf(i + 1),
                            "Parameter missing");
                    pass = false;
                }
            }
        }
        return pass;
    }

    public static boolean validateCommand(Command cmd) {
        boolean pass = true;
        boolean needTryMacro = false;
        cmd.clearValidationInfo();
        try {
            pass = tryKeyword(cmd);
        } catch (IllegalKeywordException e) {
            needTryMacro = true;
        }

        if (needTryMacro) {
            // try to treat it as a macro
            try {
                pass = tryMacro(cmd);
            } catch (InvalidMacroException e) {
                cmd.putValidationInfo(Integer.valueOf(0),
                    "Keyword '" + cmd.getKeyword() + "' is illegal");
            }
        }

        if (cmd.hasChild()) {
            for (Command child : cmd.getChildren()) {
                boolean childPassed = validateCommand(child);
                if (!childPassed) {
                    pass = false;
                }
            }
        }
        return pass;
    }
}
